﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DroughtAnalysis;

namespace DroughtAnalysis.Tests
{
    [TestClass]
    public class InterpolateDataGapsTest
    {
        [TestMethod]
        public void TestInterpolateDataGaps()
        {
            //Arrange
            DateTime start = DateTime.Now.Date;
            double oa = start.ToOADate();
            int[] indexes = new int[] { 1, 2, 3, 8, 10, 11 };
            List<DateTime> inputDates = new List<DateTime>();
            List<double> values = new List<double>();
            Random rnd = new Random();
            for(int i=0; i<indexes.Length; i++)
            {
                DateTime curT = DateTime.FromOADate(oa + indexes[i]);
                inputDates.Add(curT);
                values.Add(rnd.NextDouble());
            }
            DateTime[] inputDateArray = inputDates.ToArray();
            double[] inputValueArray = values.ToArray();

            //Act
            DataGapInterpolator interp = new DataGapInterpolator();
            IList<TimeValuePair> result = interp.InterpolateDataGaps(inputDateArray, inputValueArray, 1, -9999.0);

            //Assert
            Assert.AreEqual(result.Count, 10);
            Assert.AreEqual(inputDateArray[3], DateTime.Now.AddDays(4));
        }

        [TestMethod]
        public void TestReplaceNoDataValues()
        {
            //Arrange
            DateTime start = DateTime.Now.Date;
            double oa = start.ToOADate();
            int[] indexes = new int[] { 1, 2, 3, 8, 10, 11 };
            List<TimeValuePair> values = new List<TimeValuePair>();
            Random rnd = new Random();
            for (int i = 0; i < 10; i++)
            {
                DateTime curT = DateTime.FromOADate(oa +i);
                if (indexes.Contains(i))
                    values.Add(new TimeValuePair(curT, rnd.NextDouble()));
                else
                    values.Add(new TimeValuePair(curT, -9999.0));
            }

            //Act
            DataGapInterpolator interp = new DataGapInterpolator();
            interp.ReplaceNoDataValues(values, -9999.0);

            //Assert
            Assert.AreNotEqual(values[3].Value, -9999.0);
        }
    }
}
